/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;


/**
 * This object represents the LDAP server configuration.
 *
 */
public class JBLdapConfig
{
    /** LDAP server host name or address. */
    private String host;

    /** LDAP server port number parameter. */
    private String port;

    /** LDAP base dname. */
    private String baseDN;

    /** Applicative connections dname. */
    private String appliDN;

    /** Applicative connections password. */
    private String appliPwd;

    /** LDAP size limit. */
    private String sizeLimit;

    /** LDAP time limit. */
    private String timeLimit;

    /** Objects cache size parameter. */
    private String cacheSize;

    /** Connection type : appli or user. */
    private String connectionType;

    /** LDAP server port number. */
    private int ldapPort;

    /** Objects cache size. */
    private int objectCacheSize;

    //DW/2584/BeginPatch

    /** LDAP server sort control support. */
    private String serverSort = "false";

    //DW/2584/EndPatch

    /** Default cache size. */
    private static final int DEFAULT_CACHESIZE = 2500;

    /** Default LDAP port. */
    private static final int DEFAULT_PORT = 389;

    /**
     * Returns the applicative connections dname.
     * @return dname
     */
    public String getAppliDN()
    {
        return appliDN;
    }

    /**
     * Returns the applicative connections password.
     * @return password
     */
    public String getAppliPwd()
    {
        return appliPwd;
    }

    /**
     * Returns the LDAP base dname.
     * @return dname
     */
    public String getBaseDN()
    {
        return baseDN;
    }

    /**
     * Returns the connection type.
     * @return appli or user
     */
    public String getConnectionType()
    {
        return connectionType;
    }

    /**
     * Returns the LDAP server host name or address.
     * @return LDAP server host name or address.
     */
    public String getHost()
    {
        return host;
    }

    /**
     * Sets the applicative connections dname..
     * @param aappliDN The applicative connections dname to set
     */
    public void setAppliDN(String aappliDN)
    {
        this.appliDN = aappliDN;
    }

    /**
     * Sets the applicative connections password.
     * @param aappliPwd The applicative connections password to set
     */
    public void setAppliPwd(String aappliPwd)
    {
        this.appliPwd = aappliPwd;
    }

    /**
     * Sets the LDAP base dname.
     * @param abaseDN The base dname to set
     */
    public void setBaseDN(String abaseDN)
    {
        this.baseDN = abaseDN;
    }

    /**
     * Sets the connection type.
     * @param aconnectionType The connection type to set (appli or user)
     */
    public void setConnectionType(String aconnectionType)
    {
        this.connectionType = aconnectionType;
    }

    /**
     * Sets the LDAP server host name or address.
     * @param ahost The host name or address to set
     */
    public void setHost(String ahost)
    {
        this.host = ahost;
    }

    /**
     * Sets the LDAP server port number.
     * @param aport The port number to set.
     */
    public void setPort(String aport)
    {
        this.port = aport;

        try
        {
            ldapPort = Integer.parseInt(aport);
        }
        catch (RuntimeException e)
        {
            // default value
            ldapPort = DEFAULT_PORT;
        }
    }

    /**
     * Displays the LDAP server configuration.
     *
     * @return String formatted LDAP server configuration.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("host = ");
        buf.append(host);
        buf.append(System.getProperty("line.separator"));
        buf.append("port = ");
        buf.append(port);
        buf.append(System.getProperty("line.separator"));
        buf.append("ldapPort = ");
        buf.append(ldapPort);
        buf.append(System.getProperty("line.separator"));
        buf.append("baseDN = ");
        buf.append(baseDN);
        buf.append(System.getProperty("line.separator"));
        buf.append("appliDN = ");
        buf.append(appliDN);
        buf.append(System.getProperty("line.separator"));
        buf.append("appliPwd = ");
        buf.append(appliPwd);
        buf.append(System.getProperty("line.separator"));
        buf.append("connectionType = ");
        buf.append(connectionType);
        buf.append(System.getProperty("line.separator"));
        buf.append("cacheSize = ");
        buf.append(cacheSize);
        buf.append(System.getProperty("line.separator"));
        buf.append("objectCacheSize = ");
        buf.append(objectCacheSize);
        buf.append(System.getProperty("line.separator"));

        return buf.toString();
    }

    // end of toString method

    /**
     * Returns the LDAP server port number. Default is 389.
     * @return port number
     */
    public int getLdapPort()
    {
        return ldapPort;
    }

    /**
     * Returns the objects cache size.
     * @return objects cache size
     */
    public int getObjectCacheSize()
    {
        return objectCacheSize;
    }

    /**
     * Sets the objects cache size.
     * @param acacheSize The cache size to set
     */
    public void setCacheSize(String acacheSize)
    {
        this.cacheSize = acacheSize;

        try
        {
            objectCacheSize = Integer.parseInt(cacheSize);
        }
        catch (RuntimeException e)
        {
            // default value
            objectCacheSize = DEFAULT_CACHESIZE;
        }
    }

    /**
     * Checks whether the connections are bound in name of the user or not.
     *
     * @return true if they are, false if they are not.
     */
    public boolean isUserConnectionType()
    {
        if (connectionType == null)
        {
            return false;
        }
        else
        {
            return (connectionType.compareToIgnoreCase("user") == 0);
        }
    }

    /**
     * Checks whether the connections are bound in name of the application or not.
     *
     * @return true if they are, false if they are not.
     */
    public boolean isAppliConnectionType()
    {
        if (connectionType == null)
        {
            return true;
        }
        else
        {
            return (connectionType.compareToIgnoreCase("appli") == 0);
        }
    }

    /**
     * Returns the size limit.
     * @return size limit
     */
    public String getSizeLimit()
    {
        return sizeLimit;
    }

    /**
     * Returns the time limit.
     * @return time limit
     */
    public String getTimeLimit()
    {
        return timeLimit;
    }

    /**
     * Sets the size limit.
     * @param asizeLimit The size limit to set
     */
    public void setSizeLimit(String asizeLimit)
    {
        this.sizeLimit = asizeLimit;
    }

    /**
     * Sets the time limit.
     * @param atimeLimit The time limit to set
     */
    public void setTimeLimit(String atimeLimit)
    {
        this.timeLimit = atimeLimit;
    }

    //DW/2584/BeginPatch

    /**
     * Sets the server side sort support.
     *
     * @param value : true or false
     */
    public void setServerSort(String value)
    {
        this.serverSort = value.trim().toLowerCase();
    }

    /** Gets the server side sort support.
      *
      * @return server side sort support
      */
    public boolean isServerSortEnabled()
    {
        if (this.serverSort.equals("true"))
        {
            return true;
        }
        return false;
    }

    //DW/2584/EndPatch
}
